import dayjs from 'dayjs'

let instance

const diffFileDialog = {
  type: 'link',
  actionType: 'dialog',
  reflectAttr: { type: 'primary' },
  body: '查看',
  visibleOn: '${differenceFileUrl} !== null && !!${differenceFileUrl}',
  dialog: {
    title: '差异文件',
    body: {
      type: 'el-row',
      body: {
        type: 'for',
        items: '${billFileRespList}',
        tpl: {
          type: 'el-col',
          span: 12,
          body: {
            type: 'el-row',
            reflectAttr: {
              type: 'flex'
            },
            justify: 'center',
            style: {
              flexDirection: 'column'
            },
            body: [
              {
                type: 'tpl',
                body: '${fileName}'
              },
              {
                type: 'link',
                reflectAttr: { type: 'primary' },
                body: '下载',
                actionType: 'download',
                downloadFilePath: '${path}',
                downloadFileName: '${fileName}'
              }
            ]
          }
        }
      }
    }
  }
}

const diffDetailDialog = {
  type: 'link',
  body: '查看',
  reflectAttr: { type: 'primary' },
  visibleOn: '${predictMonthDiffList} !== null && ${predictMonthDiffList}.length !== 0',
  actionType: 'dialog',
  dialog: {
    title: '差异明细',
    width: '500px',
    body: {
      type: 'el-table',
      data: '${predictMonthDiffList}',
      body: [
        {
          type: 'el-table-column',
          label: '时间',
          width: '100px',
          prop: 'month',
          'show-overflow-tooltip': true
        },
        {
          type: 'el-table-column',
          label: '总金额差异（元）',
          prop: 'fee'
        }
      ]
    }
  }
}

export default {
  type: 'crud',
  autoRequest: false,
  border: true,
  api: {
    url: `/differenceBill/info/bill-info-list`,
    method: 'POST',
    responseData: '',
    data: {
      bussType: 'API_COST'
    }
  },
  onMounted(e) {
    instance = e.instance
  },
  querys: [
    {
      type: 'select',
      prop: 'suppliers',
      multiple: true,
      collapseTags: true,
      checkAll: true,
      label: '合作方',
      placeholder: '请选择合作方',
      api: {
        url: '/supplier/info/selectDifSupplierInfo?status=0&bussType=API_COST',
        responseAdaptor: (response) => {
          response.data = response.data.map((item) => ({
            label: item,
            value: item
          }))
          return response
        }
      },
      event: {
        loaded: (data) => {
          if (data && data.length) {
            instance.setupState.excuteQueryFn('updateDefaultValue', {
              suppliers: [data[0].value]
            })
            instance.setupState.queryEvent()
          }
        }
      }
    },
    {
      type: 'datePicker',
      prop: 'attribMonth',
      label: '归属月',
      placeholder: '请选择归属月',
      defaultValue: dayjs().format('YYYYMM'),
      'value-format': 'YYYYMM',
      reflectAttr: {
        type: 'month'
      }
    },
    {
      type: 'select',
      prop: 'billType',
      label: '单据类型',
      defaultValue: '01',
      placeholder: '请选择单据类型',
      fieldNames: { label: 'dictLabel', value: 'dictValue' },
      api: '/system/dict/data/type/t_bill_type'
    }
  ],
  columns: [
    {
      title: '合作方',
      field: 'supplier'
    },
    {
      title: '归属月',
      field: 'attribMonth'
    },
    {
      title: '单据类型',
      field: 'billType',
      body: {
        type: 'mapping',
        cache: true,
        value: '${billType}',
        api: {
          url: '/system/dict/data/type/t_bill_type',
          responseAdaptor: (response) => {
            const data = response.data
            const map = {}
            data.forEach((item) => {
              map[item.dictValue] = item.dictLabel
            })
            return {
              data: map
            }
          }
        }
      }
    },
    {
      title: '差异文件',
      field: 'diffFile',
      body: diffFileDialog
    },
    {
      title: '操作',
      field: 'operation',
      body: {
        type: 'link',
        reflectAttr: { type: 'primary' },
        actionType: 'dialog',
        dialog: {
          title: '差异分析',
          body: {
            type: 'crud',
            hasPaper: false,
            border: true,
            api: {
              url: '/differenceBill/info',
              data: {
                bussType: 'API_COST',
                billType: '02',
                supplier: '${supplier}',
                attribMonth: '${attribMonth}',
                suppliers: '${suppliers}'
              },
              responseAdaptor: (response) => {
                const rows = []
                response.data.forEach((item) => {
                  rows.push(
                    ...item.detailList.map((_item, index) => ({
                      ..._item,
                      rowspan: index === 0 ? item.detailList.length : 0,
                      billType: item.billType,
                      attribMonth: item.attribMonth,
                      supplier: item.supplier
                    }))
                  )
                })
                response.data = rows
                return response
              }
            },
            columns: [
              {
                title: '合作方',
                field: 'supplier',
                width: '100px',
                'show-overflow': true
              },
              {
                title: '单据类型',
                field: 'billType',
                width: '100px',
                body: {
                  type: 'mapping',
                  cache: true,
                  value: '${billType}',
                  api: {
                    url: '/system/dict/data/type/t_bill_type',
                    responseAdaptor: (response) => {
                      const data = response.data
                      const map = {}
                      data.forEach((item) => {
                        map[item.dictValue] = item.dictLabel
                      })
                      return {
                        data: map
                      }
                    }
                  }
                }
              },
              {
                title: '数据单号',
                field: 'billId',
                width: '150px',
                'show-overflow': true
              },
              {
                title: '数据版本',
                field: 'billDataVersion',
                width: '50px'
              },
              {
                title: '被比较数据单号',
                field: 'comparedBillId',
                width: '80px',
                'show-overflow': true
              },
              {
                title: '被比较版本',
                field: 'comparedBillDataVersion',
                width: '80px'
              },
              {
                title: '时间',
                field: 'date',
                width: '220px'
              },
              {
                title: '金额差异（元）',
                field: 'differenceAmount',
                width: '120px'
              },
              {
                title: '差异文件',
                field: 'differenceFile',
                width: '100px',
                body: diffFileDialog
              },
              {
                title: '差异明细',
                field: 'differenceDetail',
                width: '120px',
                body: diffDetailDialog
              }
            ],
            spanMethod: ({ row, columnIndex }) => {
              if (columnIndex === 0) {
                return {
                  rowspan: row.rowspan,
                  colspan: 1
                }
              }
              return {
                rowspan: 1,
                colspan: 1
              }
            }
          }
        },
        body: '差异分析'
      }
    }
  ]
}
